import AudioManager from "./AudioManager";
import PlayerDataSys from "./PlayerDataSys";
import Handler from "./Handler";
import FrameworkManager from "./frameworkManager";
import SdkHelper from "./SdkHelper";
import GameService from "./GameService";
import BaseSystem from "./BaseSystem";
import EngineUtil from "./EngineUtil";
import PageMgr from "./PageMgr";

class GameServiceMgr {
    static _instance = null;

    static _getInstance() {
        if (!GameServiceMgr._instance) {
            GameServiceMgr._instance = new GameServiceMgr();
        }
        return GameServiceMgr._instance;
    }

    logoff() {
        BaseSystem.logoff({
            yid: PlayerDataSys.yid
        }, Handler.create(this, function (res) {
            FrameworkManager.log("用户注销接口返回====", res);
            if (res && res.code === 1) {
                PageMgr.clear();
                EngineUtil.setLocalData("yid", "");
                AudioManager.getInstance().stopMusic("bgm", true);
                SdkHelper.showToast("用户已注销~");
                cc.game.restart();
            } else {
                SdkHelper.showToast(res.message || "网络异常，检查网络后重试");
            }
        }), Handler.create(this, function () {
            SdkHelper.showToast("网络异常，检查网络后重试");
        }));
    }

    recoSwitch(callback) {
        var reco_switch = PlayerDataSys.reco_switch ? 0 : 1;
        GameService.recoSwitch({
            reco_switch: reco_switch
        }, Handler.create(this, function (res) {
            FrameworkManager.log("个性化推荐====", res);
            if (res && res.code === 1) {
                PlayerDataSys.reco_switch = reco_switch;
                callback && callback();
            }
        }), Handler.create(this, function () {
            EngineUtil.showManageViewToast("网络异常,请检查网络");
        }));
    }
}

const gameServiceMgrInstance = GameServiceMgr._getInstance();

export default gameServiceMgrInstance;
